//
//  XTMainTabBarController.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/10.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit
import SVProgressHUD

class XTMainTabBarController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myTabbar = XTTabBar()

        myTabbar.xtDelegate = self
        myTabbar.tabBarBtnClosure = { ()->() in
            if XTUserAccountViewModel.sharedUserAccount.isLogin {
                self.presentComposeVC()
            }else{
                SVProgressHUD.showErrorWithStatus("登陆后才能发微博哦～")
            }
            
            
            
        }
        
        //使用kvc的方法设置只读属性
        setValue(myTabbar, forKey: "tabBar")
        
        
        addChildViewController(XTHomeTableVC(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(XTMessageTableVC(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(XTDiscoverTableVC(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(XTProfileTableVC(), title: "我的", imageName: "tabbar_profile")
        

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //重写添加自控制器的方法
    func addChildViewController(childController: UIViewController,title:String,imageName:String) {
        //设置全局统一的tabbar选中颜色
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        
        
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)//让被选中状态下的图片以原始状态进行渲染
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14)], forState: .Normal)
        //添加子控制器
            //先把创建的控制器以rootview的身份方法navVC中，
        let nav = UINavigationController(rootViewController: childController)
            //再把navVC添加到tabbarVC中
        addChildViewController(nav)
    }
    
    //弹出发微博界面的方法
    private func presentComposeVC(){
    
        let composeVC = XTComposeVC()
        let nav = UINavigationController(rootViewController: composeVC)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    
    

}


extension XTMainTabBarController : XTTabBarDelegate{
    func didSelectedComposeButton() {
        
    }
}